package com.rf.richfitwheel.admin.sys.controller;


import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.CodeRule;
import com.rf.richfitwheel.admin.sys.service.CodeRuleService;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Author bw
 * Date  2019-05-05
 */
@RestController
@RequestMapping(value = UrlConstant.WEB_ADMIN_URL+"/codeRule")
public class CodeRuleController {
    @Autowired
    private CodeRuleService codeRuleService;

    /**
     * 获取编码规则列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
    	PageUtils page = codeRuleService.queryPage(params);
		return R.ok().put("page", page);
    }

    /**
     * 根据id查询编码规则信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R get(@PathVariable("id") String id){
        CodeRule codeRule = codeRuleService.get(id);
        return R.ok().put("codeRule", codeRule);
    }

    /**
     * 添加编码规则
     * @param codeRule
     * @return
     */
    @UserOperateLog(module="系统功能",methods="insert",description="添加编码规则")
    @PostMapping("/addCodeRule")
    public R insert(@RequestBody CodeRule codeRule) {
        return codeRuleService.insert(codeRule);
    }

    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    public String insertBatch(@RequestBody List<CodeRule> codeRules) {
        if (codeRuleService.insertBatch(codeRules) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * 更新编码规则
     * @param codeRule
     * @return
     */
    @UserOperateLog(module="系统功能",methods="update",description="更新编码规则")
    @PostMapping("/update")
    public R update(@RequestBody CodeRule codeRule) {
        return codeRuleService.update(codeRule);
    }

    /**
     * 删除编码规则
     * @param ids
     * @return
     */
    @UserOperateLog(module="系统功能",methods="delete",description="删除编码规则")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        return codeRuleService.delete(ids);
    }

}
